package com.yjp.service;

import com.yjp.spring.BeanPostProcessor;
import com.yjp.spring.Component;
import com.yjp.spring.YjpApplicationContext;

import java.lang.reflect.Proxy;

@Component
public class YjpBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean){
    if (beanName.equals("userService")){
        System.out.println("111");
    }
    return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean){
        if (beanName.equals("userService")){
            Object proxyInstance = Proxy.newProxyInstance(YjpApplicationContext.class.getClassLoader(),
                    bean.getClass().getInterfaces(),
                    (proxy, method, args) -> {
                System.out.println("代理");
                return method.invoke(bean,args);

            });
            return proxyInstance;
        }
          return bean;
    }
}
